﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.Common;
using DDBuildTools.DataAccess;
using DDBuildTools.DataAccess.Components;
using Antlr.StringTemplate;
using Antlr.StringTemplate.Language;

namespace DDBuildTools
{
    /// <summary>
    /// 通用类
    /// </summary>
    public class Common
    {
        private static DataSet _GetConfig;

        static Common()
        {
            _GetConfig = new DataSet();
            _GetConfig.ReadXml("Config.xml");
        }

        /// <summary>
        /// 获得数据库连接
        /// </summary>
        /// <returns></returns>
        public List<string> GetConnList()
        {
            List<string> ConnList = new List<string>();
            foreach (DataRow var in _GetConfig.Tables["Item"].Select("Connection_ID=0"))
            {
                ConnList.Add(var["ConnSql"].ToString());
            }
            return ConnList;
        }

        /// <summary>
        /// 获得当前模板列表
        /// </summary>
        /// <returns></returns>
        public List<string> GetTemplateList()
        {
            List<string> _TemplateList = new List<string>();
            foreach (object var in TemplateList.Keys)
            {
                _TemplateList.Add(var as string);
            }
            return _TemplateList;
        }

        /// <summary>
        /// 获得数据库类型
        /// </summary>
        /// <returns></returns>
        public List<string> GetDataBaseList()
        {
            List<string> _DataBaseList = new List<string>();
            foreach (object var in DataBaseList.Keys)
            {
                _DataBaseList.Add(var as string);
            }
            return _DataBaseList;
        }

        /// <summary>
        /// 获得模板路径
        /// </summary>
        public static SortedList TemplateList
        {
            get
            {
                SortedList TemplateList = new SortedList();
                foreach (DataRow var in _GetConfig.Tables["Item"].Select("Template_ID=0"))
                {
                    TemplateList.Add(var["name"].ToString(), var["Path"].ToString());
                }
                return TemplateList;
            }
        }

        /// <summary>
        /// 模板处理类
        /// </summary>
        public static Hashtable TemplatetypeNameList
        {
            get {
                Hashtable TemplatetypeNameList = new Hashtable();
                foreach (DataRow var in _GetConfig.Tables["Item"].Select("Template_ID=0"))
                {
                    TemplatetypeNameList.Add(var["name"].ToString(), var["typeName"].ToString());
                }
                return TemplatetypeNameList;
            }
        }

        /// <summary>
        /// 数据库列表
        /// </summary>
        public static Hashtable DataBaseList
        {
            get
            {
                Hashtable DataBaseList = new Hashtable();
                foreach (DataRow var in _GetConfig.Tables["Item"].Select("DataBase_ID=0"))
                {
                    DataBaseList.Add(var["DBName"].ToString(), var["typeName"].ToString());
                }
                return DataBaseList;
            }
        }
        
        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="FileTxt"></param>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static bool WriteFile(string FileTxt, string FilePath)
        {
            bool rBool = false;

            try
            {
                System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(FilePath));
                FileStream fs = new FileStream(FilePath, FileMode.Create, FileAccess.ReadWrite);

                StreamWriter w = new StreamWriter(fs, Encoding.UTF8);
                //w.BaseStream.Seek(0, SeekOrigin.End);
                w.Write(FileTxt);
                w.Flush();
                w.Close();
                w.Dispose();
                fs.Close();
                fs.Dispose();
            }
            catch
            {
                rBool = false;
            }


            return rBool;
        }

        /// <summary>
        /// 复制文件夹
        /// </summary>
        /// <param name="SourcePath"></param>
        /// <param name="DestinPath"></param>
        public static void CopyFolder(string SourcePath, string DestinPath)
        {
            if (!Directory.Exists(DestinPath))
            {
                Directory.CreateDirectory(DestinPath);
            }
            DirectoryInfo rootdir = new DirectoryInfo(SourcePath);

            //遍历文件
            FileInfo[] fileinfo = rootdir.GetFiles();
            foreach (FileInfo file in fileinfo)
            {
                file.CopyTo(DestinPath + "\\" + file.Name, true);
            }

            //递归
            DirectoryInfo[] childdir = rootdir.GetDirectories();
            foreach (DirectoryInfo dir in childdir)
            {
                CopyFolder(dir.FullName, DestinPath + "\\" + dir.Name);
            }
        }

        /// <summary>
        /// 补0函数
        /// </summary>
        /// <param name="j"></param>
        /// <returns></returns>
        public static string BuildZero(int j)
        {
            string _Zero = "";
            for (int i = 0; i < j; i++)
            {
                _Zero = _Zero + "0";
            }
            return _Zero;
        }


    }
}
